﻿Public Class frmPriceCalcList
    Dim dbBase As New dbPriceCalc
    Dim frmCtrl As New FormControl
    Dim dsBase As DataSet
    Dim EntryID As String
    Dim CopyText As String

    Private Sub frmPriceCalcList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        EntryID = ""
        dsBase = dbBase.GetBaseList("")
        ToolbarHeader1.DisableCMS = True
        ToolbarHeader1.Iniliaze("LIST : Price Calculation", Me, Nothing, dgvBase, "Code")
        If (dsBase IsNot Nothing) Then
            dgvBase.Rows.Clear()
            For Each row As DataRow In dsBase.Tables(0).Rows
                dgvBase.Rows.Add(row.ItemArray)
            Next row
        End If
        ToolbarFind1.Initialize_Tools(dsBase, dgvBase)
        ToolbarHeader1.ActionCtrl(True, False, True, False, True, False, False, True, True)
        ToolbarHeader1.Visible = True
        dgvBase.ContextMenuStrip = cmsBase
    End Sub

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String)
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[Code] = '" & KeyValue & "'"
        EntryID = dvFind.Item(0).Row("Code").ToString()
    End Sub

    Private Sub GridDoubleClick_Click(ByVal sender As GridAndFind)
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        Dim OpenForm As New frmPriceCalculation
        OpenForm.EntryID = EntryID
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            Dim result As Boolean '= dbBase.deleteJournalGL(EntryID, Var.CompID)
            If (result) Then
                frmCtrl.openChild(New frmPriceCalcList, Me.ParentForm)
            Else : MessageBox.Show("Error in Delete Progress")
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As Object) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmPriceCalcList, Me.ParentForm)
    End Sub

    Private Sub dgvBase_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvBase.CellEnter
        EntryID = dgvBase.Rows(e.RowIndex).Cells(0).Value.ToString()
    End Sub

    Private Sub dgvBase_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvBase.CellMouseDown
        If (e.RowIndex >= 0 And e.Button = Windows.Forms.MouseButtons.Right) Then
            dgvBase.Rows(e.RowIndex).Selected = True
            dgvBase.CurrentCell = dgvBase.Rows(e.RowIndex).Cells(0)
            EntryID = dgvBase.Rows(e.RowIndex).Cells(0).Value.ToString()
            CopyText = dgvBase.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
            If (dgvBase.Rows(e.RowIndex).Cells("Stat").Value.ToString() = "I") Then
                SetActive.Enabled = True
            Else : SetActive.Enabled = False
            End If
        End If
    End Sub

    Private Sub dgvBase_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvBase.KeyDown
        If (dgvBase.RowCount > 0) Then
            If (e.KeyCode = Keys.Enter) Then
                tsEdit_Click(Nothing)
            End If
        End If
    End Sub

    Private Sub dgvBase_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvBase.CellMouseDoubleClick
        If (dgvBase.RowCount > 0 And e.Button = Windows.Forms.MouseButtons.Left) Then
            tsEdit_Click(Nothing)
        End If
    End Sub

    Private Sub cmsBase_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles cmsBase.ItemClicked
        Select Case e.ClickedItem.Text
            Case "&New"
                If (dgvBase IsNot Nothing) Then
                    tsNew_Click(Nothing)
                End If
            Case "&Edit"
                If (dgvBase IsNot Nothing) Then
                    tsEdit_Click(Nothing)
                End If
            Case "&Refresh"
                tsRefresh_Click(Nothing)
            Case "&Delete"
                If (dgvBase IsNot Nothing) Then
                    Dim rowI As Integer = dgvBase.CurrentRow.Index
                    Dim answer As DialogResult
                    answer = MessageBox.Show("Are you sure deleting data. " + dgvBase.Item(0, rowI).Value.ToString().Trim() + " ?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                    If answer = vbYes Then
                        tsDelete_Click(Nothing)
                    End If
                End If
            Case "&Close"
                Me.Close()
            Case "Copy"
                Clipboard.SetText(CopyText)
        End Select
        If (e.ClickedItem.Name = SetActive.Name) Then
            MessageBox.Show("")
        End If
    End Sub

    Private Sub dgvBase_Sorted(sender As Object, e As EventArgs) Handles dgvBase.Sorted
        If (dgvBase.Columns.Count > 0) Then
            ToolbarFind1.cbFind.SelectedIndex = dgvBase.SortedColumn.HeaderCell.ColumnIndex
            ToolbarFind1.txtFind.Focus()
        End If
    End Sub
End Class